package com.xcexample.electronicclasscard.kaoqin

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.*
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.GsonBuilder
import com.xcexample.electronicclasscard.log.LogUtils
import com.xcexample.electronicclasscard.databinding.LayoutWorkHistoryBinding
import com.xcexample.electronicclasscard.net.ApiFactory
import com.xcexample.electronicclasscard.net.BaseURL
import okhttp3.ResponseBody
import org.json.JSONException
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class WorkHistoryFragment : DialogFragment() {

    private var _binding: LayoutWorkHistoryBinding? = null

    val workAdapter by lazy {
        WorkAdapter()
    }

    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = LayoutWorkHistoryBinding.inflate(inflater, container, false)


        return binding.root

    }

    var classNoticePage = 1


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        binding.rvWork.adapter = workAdapter
        binding.ivClose.setOnClickListener {
            dismiss()
        }


        binding.pre.setOnClickListener {
            if (classNoticePage == 1) {
                ToastUtils.showShort("没有上一页了哦")
                return@setOnClickListener
            }
            classNoticePage -= 1
            getData()
        }
        binding.next.setOnClickListener {
            if (classNoticePage == lastPage) {
                ToastUtils.showShort("没有下一页了哦")
                return@setOnClickListener
            }
            classNoticePage += 1
            getData()
        }

        getData()
    }

    var lastPage = 0
    private fun getData() {
        var retrofit = ApiFactory.create(BaseURL,true)
        var call = retrofit.getKaoqinlog("$classNoticePage")
        call.enqueue(object : Callback<ResponseBody?> {
            override fun onResponse(
                call: Call<ResponseBody?>,
                response: Response<ResponseBody?>
            ) {
                val json = response.body()?.string()
                try {
                    val gson = GsonBuilder().create()
                    val obj = gson.fromJson(json, WorkHistoryBean::class.java)
                    LogUtils.e("result:$obj")
                    if (obj.code == 0) {
                        var result =obj.data
                        workAdapter.setList(result?.data)
                        lastPage = result?.last_page ?: 0
                        binding.total.text = "${result?.total}"
                    } else {
                        Toast.makeText(this@WorkHistoryFragment.context, obj.msg, Toast.LENGTH_SHORT)
                            .show()
                    }

                } catch (e: JSONException) {
                    e.printStackTrace()
                }

            }

            override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {

            }
        })
//        LoginRepository.getData(
//            LoginRepository.apiService.getKaoqinlog(
//                ModelParams.SchoolNotice(page = "$classNoticePage")
//            ), object : BaseRepository.ResultListener {
//                override fun onSuccess(result: Any) {
////                    workAdapter.setList(result?.data)
////                    lastPage = result?.last_page ?: 0
////                    binding.total.text = "${result?.total}"
//                }
//
//                override fun onError(e: Throwable) {
//
//                }
//
//            })
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }


    override fun onStart() {
        super.onStart()
        val displayMetrics = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
        val width = displayMetrics.widthPixels * 0.83
        val height = displayMetrics.heightPixels * 0.73
        dialog?.window?.setLayout(width.toInt(), height.toInt())
    }
}